<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\TagRequest;
use App\Models\Tag;
use Illuminate\Http\Request;

class TagController extends Controller
{
    public function index(){
        $tags = Tag::orderByDesc('id')->paginate(request('page_size', 10));

        return view('backend.tag.index', compact('tags'));
    }

    public function store(
        TagRequest $request,
        Tag $tag
    ){
        $tag->fill($request->only(['tag']))->save();

        flash('Tag添加成功', 'success');

        return back();
    }

    public function edit($id){
        $tag = Tag::findOrFail($id);
        return view('backend.tag.edit',compact('tag'));
    }

    public function update(TagRequest $request,$id){
        $tag = Tag::findOrFail($id);
        $tag->fill($request->only(['tag']))->save();
        flash('tag编辑成功', 'success');
        return back();
    }

    public function destroy($id)
    {
        //TODO::判断该tag下是否有文章
        $tag = Tag::findOrFail($id);

        $tag->delete();
        flash('tag删除成功', 'success');
        //}

        return back();
    }
}
